package com.gxa.learning.portal.controller;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.pojo.ProjectDetails;
import com.gxa.learning.core.pojo.Type;
import com.gxa.learning.portal.mapper.ProjectDetailsMapper;
import com.gxa.learning.portal.service.ProjectDetailsService;
import com.gxa.learning.portal.service.ProjectService;
import com.gxa.learning.portal.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 我的主页的相关接口
 *
 * @Author lirucong
 * @Date 16:45 2021/12/9
 */
@Api("我的主页的相关接口")
@Controller
@ResponseBody
@RequestMapping("/main")
public class MainPageController {

    @Autowired
    private TypeService typeService = null;

    @Autowired
    private ProjectDetailsService projectDetailsService = null;

    @Autowired
    private ProjectService projectService = null;


    @ApiOperation("获取所有的类型列表")
    @GetMapping("/getTypes")
    public Map<String, Object> getTypes() throws BusinessException {
        List<Type> types = typeService.selectAll();
        if (types.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("成功");
        jsonResult.setCode("1");
        jsonResult.setData(types);
        return jsonResult.getDataMap();
    }

    @ApiOperation("根据tid找到所有的项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid", value = "tid", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "page", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "size", required = true, paramType = "query", dataType = "Integer")
    })
    @GetMapping("/getProjectByType")
    public Map<String, Object> getProjectByType(Long tid, Integer page, Integer size) throws BusinessException {
        List<ProjectDetails> project = typeService.getProjectByType(tid, page, size);
        Integer count = typeService.getProjectByTypeCount(tid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("成功");
        jsonResult.setCode("1");
        jsonResult.setData(project);
        jsonResult.put("count", count);
        return jsonResult.getDataMap();
    }

    @ApiOperation("根据id获取对应的项目")
    @ApiImplicitParam(name = "pid", value = "pid", required = true, paramType = "query", dataType = "long")
    @GetMapping("/getProDetailsById")
    public Map<String, Object> getProjectDetails(Long pid) throws BusinessException {
        ProjectDetails byId = projectDetailsService.getById(pid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("成功");
        jsonResult.setCode("1");
        jsonResult.setData(byId);
        return jsonResult.getDataMap();
    }

    @ApiOperation("搜索")
    @GetMapping("/search")
    @ApiImplicitParam(name = "condition", value = "condition", dataTypeClass = String.class, dataType = "String", paramType = "query")
    public Map<String, Object> search(String condition) throws BusinessException {
        List<ProjectDetails> search = projectDetailsService.search(condition);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("成功");
        jsonResult.setCode("1");
        jsonResult.setData(search);
        return jsonResult.getDataMap();
    }
}
